package is.olsenolsen;

import java.util.ArrayList;
import java.util.Collections;
import is.Exceptions.EmptyDeckException;


public class Deck {
	ArrayList<Card> cards = new ArrayList<Card>();
	ArrayList<Card> discards = new ArrayList<Card>();
	
	public Deck(int size){
		char[] suits = {'h', 's', 'd', 'c'};
		int[] values = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
		ArrayList<Card> temp = new ArrayList<Card>();
		
		for (int j = 0; j < 4; j++){
			for (int i = 0; i < 13; i++){
				temp.add(new Card(values[i], suits[j]));
			}			
		}
		Collections.shuffle(temp);
		
		int i = 0;
		while (i < size){
			cards.add(temp.remove(0));
			i++;
		}
	}
	
	//Notkun: shuffle(cardDeck)
	//Fyrir:  cardDeck er ArrayList sem inniheldur
	//        Card-hluti.
	//Eftir:  cardDeck inniheldur alla somu Card-hluti
	//        en rodun theirra er breytt.
	public void shuffle(){
		cards.addAll(discards);
		cards.trimToSize();
		Collections.shuffle(cards);
	}
	
	//Notkun: b = draw(n)
	//Fyrir:  n er heiltala
	//Eftir:  b ArrayList<Card> med n-stok
	//        og deck.cards er med n-faerri stok.
	public ArrayList<Card> draw(int nrCards) throws EmptyDeckException{
		ArrayList<Card> temp = new ArrayList<Card>();
		for(int i = 0; i < nrCards; i++){
			if(this.cards.isEmpty()){
				throw new EmptyDeckException();
			}
			temp.add(this.cards.remove(0));
		}
		return temp;
	}

	//Notkun: putback(card)
	//Fyrir:  card er hlutur af tagi Card
	//Eftir:  card er komid i stokkinn
	public void putback(Card card){
		discards.add(card);
		discards.trimToSize(); //Tom stok fjarlaegd
	}
	
}